module.exports = class extends baseLogic {
    constructor() {
        super();

    }

    static init() {
        return new this();
    }

    async getItems(FindFilterModel, curPage, pageSize, sort,curUserInfo) {
        let self = this;
        let Result = {};
        Result.CurPage = curPage;
        Result.PageSize = pageSize;
        let FindFilter ={};
        if (FindFilterModel.ct_title !=='') {
            FindFilter.ct_title = {
                '$like': '%' + FindFilterModel.ct_title + '%'
            };
        }

        if (FindFilterModel.ct_sign !=='') {
            FindFilter.ct_sign = {
                '$like': '%' + FindFilterModel.ct_sign + '%'
            };
        }

        if(!isNaN(FindFilterModel.ct_org_id)){
            FindFilter.ct_org_id =FindFilterModel.ct_org_id;
        }

        if (FindFilterModel.ct_create_time) {
            FindFilter.ct_create_time = {
                '$between': FindFilterModel.ct_create_time.split(',')
            };
        }
        let count = await  this.DB.iframe_custom_theme.Access.count({
            where: FindFilter
        });

        this.DB.iframe_custom_theme.Access.belongsTo(this.DB.iframe_orgstruct.Access,{foreignKey:'ct_org_id'});
        let data = await this.DB.iframe_custom_theme.Access.findAndCountAll({
            where: FindFilter,
            include:[{
                attributes:['OS_Name'],
                model:this.DB.iframe_orgstruct.Access,
            }],
            order: [
                sort.split('|')
            ],
            offset: (curPage - 1) * pageSize,
            limit: pageSize,
            raw: true
        });

        Result.RsCount =count;
        Result.DataList = data.rows;
        Result.OrgStruct = await this.func.orgStruct.getPartnersItemsTreeData(null,curUserInfo); //根据各自的数据可视化程度 获取用户可见的组织结构的树形结构数据
        return Result;
    }

    async getItem(key){
        let ret = await this.DB.iframe_custom_theme.Access.findOne({
            where:{
                ct_id:key
            },
            raw:true
        });
        return ret;
    }

    async getItemBySign(sign){
        let ret  = await this.DB.iframe_custom_theme.Access.findOne({
            where: {
                ct_sign: sign
            },
            raw:true
        });
        return ret;
    }

    async insertOrUpdate(body,userInfo){

        var ct_id = body.ct_id || '';
        var model={};
        if(ct_id && ct_id!=''){
            model = await this.DB.iframe_custom_theme.Access.findById(ct_id);
            if(!model){
                return null;
            }
            model = model.dataValues;
        }else{
            model.ct_id= this.webTools.myuuid.createUUID();
            model.ct_create_time = this.moment().format("YYYY-MM-DD HH:mm:ss")
        }
        model.ct_title = body.ct_title;
        model.ct_sign = body.ct_sign;
        model.ct_system_name = body.ct_system_name;
        model.ct_org_id=body.ct_org_id || userInfo.UserPartnersID;
        model.ct_login_bg=body.ct_login_bg;
        model.ct_logo_b=body.ct_logo_b;
        model.ct_logo_s=body.ct_logo_s;

        if (ct_id!=='') {
            model= await this.DB.iframe_custom_theme.Access.update(model, {
                where: {
                    ct_id: ct_id
                }
            });
        } else {
            model = await this.DB.iframe_custom_theme.Access.create(model);
        }
        return model;
    }

    async del(keys){
        return await this.DB.iframe_custom_theme.Access.destroy({
            where: {
                ConfigKey: keys
            }
        });
    }

    async checkSign(ct_id,ct_sign){
        let count=await this.DB.iframe_custom_theme.Access.count({
            where:{ct_id:{'$ne':ct_id},ct_sign:ct_sign},
            raw:true
        });
        return count;
    }


}
